import datetime

from peewee import AutoField, DateTimeField, Model, MySQLDatabase
from playhouse.shortcuts import model_to_dict
from playhouse.pool import PooledMySQLDatabase

from config.config import db_host, db_name, db_passwd, db_port, db_user

# db = MySQLDatabase(
#     database=db_name, host=db_host, port=db_port, user=db_user, passwd=db_passwd, charset="utf8mb4"
# )

db = PooledMySQLDatabase(database=db_name, host=db_host, port=db_port, user=db_user, passwd=db_passwd, charset="utf8mb4", max_connections=20)

class BaseModel(Model):
    id = AutoField()
    create_at = DateTimeField(default=datetime.datetime.now)
    update_at = DateTimeField(default=datetime.datetime.now)

    class Meta:
        database = db

    def to_dict(self):
        exclude = set()
        if getattr(self, "__exclude__", None):
            for exclude_field_name in self.__exclude__:
                exclude.add(self._meta.combined[exclude_field_name])
        return model_to_dict(self, exclude=exclude)
